/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package View;

import Model.Item;
import java.util.ArrayList;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

/**
 *
 * @author kostas
 * I have no idea why do I need this class...
 */
public class ItemTableModel implements TableModel {
    private ArrayList<Item> items;
    public ItemTableModel(ArrayList<Item> i) {
        items = i;
    }

    public int getRowCount() {
        return items.size();
    }

    public int getColumnCount() {
        return 2; // Because we have only item Id and product name in the table
    }

    public ArrayList<Item> getItems() {
        return items;
    }
    
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "Item ID";
            case 1:
                return "Product name";
            default:
                return "";
        }
    }

    public void addItem(Item i) {
        items.add(i);
    }

    public void removeItem(int index) {
        items.remove(index);
    }
    public void clear() {
        items.clear();
    }
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
        // This is a strange method.. I wonder what is it used for...
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
        // We don't want the table cells to be editable.
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Item item = items.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return "" + item.getItemId();
            case 1:
                return item.getProduct().getName();
            default:
                return null;
        }
    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        // Setting is for editable cells which are not available in this model
    }

    public void addTableModelListener(TableModelListener l) {
        // Not used
    }

    public void removeTableModelListener(TableModelListener l) {
        // And this
    }

}
